package com.generator.model;

import lombok.Data;
import lombok.Getter;
import lombok.Setter;

/**
 * 数据库列信息
 * 
 * @author Generator
 * @since 2024-01-01
 */
@Data
public class ColumnInfo {

    // Getter and Setter methods
    /** 列名 */
    private String columnName;
    
    /** 数据库类型 */
    private String dbType;
    
    /** Java类型 */
    private String javaType;
    
    /** 属性名 */
    private String propertyName;
    
    /** 注释 */
    private String comment;
    
    /** 是否主键 */
    private boolean primaryKey;
    
    /** 是否自增 */
    private boolean autoIncrement;
    
    /** 是否允许为空 */
    private boolean nullable;
    
    /** 默认值 */
    private String defaultValue;
    
    /** 长度 */
    private Integer length;
    
    /** 精度 */
    private Integer precision;
    
    /** 小数位数 */
    private Integer scale;

    public ColumnInfo() {}

    public ColumnInfo(String columnName, String dbType) {
        this.columnName = columnName;
        this.dbType = dbType;
        this.propertyName = toCamelCase(columnName);
        this.javaType = mapDbTypeToJavaType(dbType);
    }

    /**
     * 将下划线命名转换为驼峰命名
     */
    private String toCamelCase(String columnName) {
        if (columnName == null || columnName.isEmpty()) {
            return columnName;
        }
        
        StringBuilder result = new StringBuilder();
        boolean nextUpperCase = false;
        
        for (char c : columnName.toCharArray()) {
            if (c == '_') {
                nextUpperCase = true;
            } else if (nextUpperCase) {
                result.append(Character.toUpperCase(c));
                nextUpperCase = false;
            } else {
                result.append(Character.toLowerCase(c));
            }
        }
        
        return result.toString();
    }

    /**
     * 数据库类型映射到Java类型
     */
    private String mapDbTypeToJavaType(String dbType) {
        if (dbType == null) return "String";
        
        String type = dbType.toLowerCase();
        
        if (type.contains("int") || type.contains("tinyint")) {
            return "Integer";
        } else if (type.contains("bigint")) {
            return "Long";
        } else if (type.contains("decimal") || type.contains("numeric")) {
            return "BigDecimal";
        } else if (type.contains("float")) {
            return "Float";
        } else if (type.contains("double")) {
            return "Double";
        } else if (type.contains("date")) {
            return "LocalDate";
        } else if (type.contains("time")) {
            return "LocalDateTime";
        } else if (type.contains("timestamp")) {
            return "LocalDateTime";
        } else if (type.contains("bit") || type.contains("boolean")) {
            return "Boolean";
        } else {
            return "String";
        }
    }

    /**
     * 获取属性名的首字母大写形式
     */
    public String getPropertyNameCapitalized() {
        if (propertyName == null || propertyName.isEmpty()) {
            return propertyName;
        }
        return Character.toUpperCase(propertyName.charAt(0)) + propertyName.substring(1);
    }

    /**
     * 判断是否需要导入的Java类型
     */
    public boolean needImport() {
        return "LocalDate".equals(javaType) || "LocalDateTime".equals(javaType) || "BigDecimal".equals(javaType);
    }

    @Override
    public String toString() {
        return "ColumnInfo{" +
                "columnName='" + columnName + '\'' +
                ", dbType='" + dbType + '\'' +
                ", javaType='" + javaType + '\'' +
                ", propertyName='" + propertyName + '\'' +
                ", comment='" + comment + '\'' +
                ", primaryKey=" + primaryKey +
                '}';
    }
}